package com.baojun.newterritory.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import com.baojun.checkout.di.CheckoutBuilder;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.lang.ref.WeakReference;

/**
 * 微信分享回调
 *
 * @author 刘利强
 * @date 2019/1/16
 */
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    private IWXAPI api;
    private MyHandler handler;

    private static class MyHandler extends Handler {
        private final WeakReference<WXEntryActivity> wxEntryActivityWeakReference;

        public MyHandler(WXEntryActivity wxEntryActivity) {
            wxEntryActivityWeakReference = new WeakReference<WXEntryActivity>(wxEntryActivity);
        }
    }

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        api = WXAPIFactory.createWXAPI(this, "wxedfa5d9c6ed20a47", false);
        handler = new MyHandler(this);

        try {
            Intent intent = getIntent();
            api.handleIntent(intent, this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {
        Log.d("wyj", "req type:" + baseReq.getType());
        this.finish();
//        Intent intent = new Intent(this, HomeActivity.class);
//        startActivity(intent);
//        JumpPageUtils.goCommonWeb(this, WebUrlConfigConstant.MY_RED_PACKET);
    }

    @Override
    public void onResp(BaseResp baseResp) {
//        Intent mIntent = new Intent(this, HomeActivity.class);
//        startActivity(mIntent);
        //如果是小程序
        if (baseResp.getType() == ConstantsAPI.COMMAND_LAUNCH_WX_MINIPROGRAM) {
            //
            int status = 0;
            try {
                WXLaunchMiniProgram.Resp resp = (WXLaunchMiniProgram.Resp) baseResp;
                status = getPayStatus(resp.extMsg);
            } catch (Exception e) {
                e.printStackTrace();
            }

            CheckoutBuilder builder = new CheckoutBuilder(this);
            builder.showResult(status != 2);
//            Intent intent = new Intent(this, CommonWebActivity.class);
//            intent.putExtra("wx_miniprogram", 200);
//            intent.putExtra("status", status);
//            startActivity(intent);
        }

        // TODO 微信分享 成功之后调用接口
        this.finish();

    }

    /**
     * 获取支付状态
     * 0 未定义 1：成功 2：失败 3:其它
     */
    private int getPayStatus(String extmsg) {
        int status = 0;
        if (!TextUtils.isEmpty(extmsg)) {
            String[] params = extmsg.split("&");
            for (String param : params) {
                if (param != null && param.startsWith("status")) {
                    String[] statusParam = param.split("=");
                    if (statusParam.length > 1) {
                        switch (statusParam[1]) {
                            case "success":
                                status = 1;
                                break;
                            case "fail":
                                status = 2;
                                break;
                            default:
                                status = 3;
                                break;
                        }
                    }
                }
            }
        }
        return status;
    }
}
